home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir42 / gnudbm14.zip / FALLOC.C < prev    next >
C/C++ Source or Header  |  1990-08-24  |  13KB  |  448 lines

  1. /* falloc.c - The file space management routines for dbm. */
  2.  
  3. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  4.     Copyright (C) 1990  Free Software Foundation, Inc.
  5.  
  6.     GDBM is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 1, or (at your option)
  9.     any later version.
  10.  
  11.     GDBM is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with GDBM; see the file COPYING.  If not, write to
  18.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     You may contact the author by:
  21.        e-mail:  phil@wwu.edu
  22.       us-mail:  Philip A. Nelson
  23.                 Computer Science Department
  24.                 Western Washington University
  25.                 Bellingham, WA 98226
  26.         phone:  (206) 676-3035
  27.        
  28. *************************************************************************/
  29.  
  30. /*
  31.  * MS-DOS port (c) 1990 by Thorsten Ohl, td12@@ddagsi3.bitnet
  32.  *
  33.  * To this port, the same copying conditions apply as to the
  34.  * original release.
  35.  *
  36.  * IMPORTANT:
  37.  * This file is not identical to the original GNU release!
  38.  * You should have received this code as patch to the official
  39.  * GNU release.
  40.  *
  41.  * MORE IMPORTANT:
  42.  * This port comes with ABSOLUTELY NO WARRANTY.
  43.  *
  44.  * $Header: e:/gnu/gdbm/RCS/falloc.c'v 1.4.0.1 90/08/16 09:22:19 tho Exp $
  45.  */
  46.  
  47. #include <stdio.h>
  48. #include <sys/types.h>
  49. #ifndef MSDOS
  50. #include <sys/file.h>
  51. #endif /* not MSDOS */
  52. #include <sys/stat.h>
  53. #include "gdbmdefs.h"
  54. #include "systems.h"
  55.  
  56.  
  57. #ifdef __STDC__
  58. static avail_elem get_elem (int size, avail_elem *av_table, int *av_count);
  59. static avail_elem get_block (int size, gdbm_file_info *dbf);
  60. static VOID push_avail_block (gdbm_file_info *dbf);
  61. static VOID pop_avail_block (gdbm_file_info *dbf);
  62. static VOID adjust_bucket_avail (gdbm_file_info *dbf);
  63. #else /* not __STDC__ */
  64. static avail_elem get_elem ();    /* Get avail_elems from a list. */
  65. static avail_elem get_block ();
  66. static push_avail_block ();
  67. static pop_avail_block ();
  68. static adjust_bucket_avail ();
  69. #endif /* not __STDC__ */
  70.  
  71. /* Allocate space in the file DBF for a block NUM_BYTES in length.  Return
  72.    the file address of the start of the block.  
  73.  
  74.    Each hash bucket has a fixed size avail table.  We first check this
  75.    avail table to satisfy the request for space.  In most cases we can
  76.    and this causes changes to be only in the current hash bucket.
  77.    Allocation is done on a first fit basis from the entries.  If a
  78.    request can not be satisfied from the current hash bucket, then it is
  79.    satisfied from the file header avail block.  If nothing is there that
  80.    has enough space, another block at the end of the file is allocated
  81.    and the unused portion is returned to the avail block.  This routine
  82.    "guarantees" that an allocation does not cross a block boundary unless
  83.    the size is larger than a single block.  The avail structure is
  84.    changed by this routine if a change is needed.  If an error occurs,
  85.    the value of 0 will be returned.  */
  86.  
  87. LONG
  88. _gdbm_alloc (dbf, num_bytes)
  89.      gdbm_file_info *dbf;
  90.      int num_bytes;
  91. {
  92.   LONG file_adr;            /* The address of the block. */
  93.   avail_elem av_el;        /* For temporary use. */
  94.  
  95.   /* The current bucket is the first place to look for space. */
  96.   av_el = get_elem (num_bytes, dbf->bucket->bucket_avail,
  97.             &dbf->bucket->av_count);
  98.  
  99.   /* If we did not find some space, we have more work to do. */
  100.   if (av_el.av_size == 0)
  101.     {
  102.      /* Is the header avail block empty and there is something on the stack. */
  103.       if (dbf->header->avail.count == 0 && dbf->header->avail.next_block != 0)
  104.     pop_avail_block (dbf);
  105.       
  106.       /* Get another full block from end of file. */
  107.       av_el = get_block (num_bytes, dbf);
  108.  
  109.       dbf->header_changed = TRUE;
  110.     }
  111.  
  112.   /* We now have the place from which we will allocate the new space. */
  113.   file_adr = av_el.av_adr;
  114.  
  115.   /* Put the unused space back in the avail block. */
  116.   av_el.av_adr += num_bytes;
  117.   av_el.av_size -= num_bytes;
  118.   _gdbm_free (dbf, av_el.av_adr, av_el.av_size);
  119.  
  120.   /* Return the address. */
  121.   return file_adr;
  122.   
  123. }
  124.  
  125.  
  126.  
  127. /* Free space of size NUM_BYTES in the file DBF at file address FILE_ADR.  Make
  128.    it avaliable for reuse through _gdbm_alloc.  This routine changes the
  129.    avail structure.  The value TRUE is returned if there were errors.  If no
  130.    errors occured, the value FALSE is returned. */
  131.  
  132. _gdbm_free (dbf, file_adr, num_bytes)
  133.      gdbm_file_info *dbf;
  134.      LONG file_adr;
  135.      int num_bytes;
  136. {
  137.   avail_elem temp;
  138.  
  139.   /* Is it too small to worry about? */
  140.   if (num_bytes <= IGNORE_SIZE)
  141.     return TRUE;        /* is that correct (was "return;")? -tho */
  142.  
  143.   /* Initialize the avail element. */
  144.   temp.av_size = num_bytes;
  145.   temp.av_adr = file_adr;
  146.  
  147.   /* Is the freed space large or small? */
  148.   if (num_bytes >= dbf->header->block_size)
  149.     {
  150.       if (dbf->header->avail.count == dbf->header->avail.size)
  151.     {
  152.       push_avail_block (dbf);
  153.     }
  154.       _gdbm_put_av_elem (temp, dbf->header->avail.av_table,
  155.              &dbf->header->avail.count);
  156.       dbf->header_changed = TRUE;
  157.     }
  158.   else
  159.     {
  160.       /* Try to put into the current bucket. */
  161.       if (dbf->bucket->av_count < BUCKET_AVAIL)
  162.     _gdbm_put_av_elem (temp, dbf->bucket->bucket_avail,
  163.                &dbf->bucket->av_count);
  164.       else
  165.     {
  166.       if (dbf->header->avail.count == dbf->header->avail.size)
  167.         {
  168.           push_avail_block (dbf);
  169.         }
  170.       _gdbm_put_av_elem (temp, dbf->header->avail.av_table,
  171.                  &dbf->header->avail.count);
  172.       dbf->header_changed = TRUE;
  173.     }
  174.     }
  175.  
  176.   if (dbf->header_changed)
  177.     adjust_bucket_avail (dbf);
  178.  
  179.   /* All work is done. */
  180.   return TRUE;            /* is that correct (was "return;")? -tho */
  181. }
  182.  
  183.  
  184.  
  185.  
  186. /* The following are all utility routines needed by the previous two. */
  187.  
  188.  
  189. /* Gets the avail block at the top of the stack and loads it into the
  190.    active avail block.  It does a "free" for itself! */
  191.  
  192. static VOID
  193. pop_avail_block (dbf)
  194.      gdbm_file_info *dbf;
  195. {
  196.   LONG num_bytes;        /* For reading. */
  197.   avail_elem temp;
  198.  
  199.   /* Set up variables. */
  200.   temp.av_adr = dbf->header->avail.next_block;
  201.   temp.av_size = ( ( (dbf->header->avail.size * sizeof (avail_elem)) >> 1)
  202.           + sizeof (avail_block));
  203.  
  204.   /* Read the block. */
  205.   num_bytes = lseek (dbf->desc, temp.av_adr, L_SET);
  206.   if (num_bytes != temp.av_adr)  _gdbm_fatal (dbf, "lseek error");
  207. #ifdef MSDOS            /* shut up the compiler!  */
  208.   num_bytes = read (dbf->desc,
  209.             (char *) &dbf->header->avail, temp.av_size);
  210. #else /* not MSDOS */
  211.   num_bytes = read (dbf->desc, &dbf->header->avail, temp.av_size);
  212. #endif /* not MSDOS */
  213.   if (num_bytes != temp.av_size) _gdbm_fatal (dbf, "read error");
  214.  
  215.   /* We changed the header. */
  216.   dbf->header_changed = TRUE;
  217.  
  218.   /* Free the previous avail block. */
  219.   _gdbm_put_av_elem (temp, dbf->header->avail.av_table,
  220.              &dbf->header->avail.count);
  221. }
  222.  
  223.  
  224. /* Splits the header avail block and pushes half onto the avail stack. */
  225.  
  226. static VOID
  227. push_avail_block (dbf)
  228.      gdbm_file_info *dbf;
  229. {
  230.   LONG num_bytes;
  231.   int av_size;
  232.   LONG av_adr;
  233.   int index;
  234.   avail_block *temp;
  235.   avail_elem  new_loc;
  236.  
  237.  
  238.   /* Caclulate the size of the split block. */
  239.   av_size = ( (dbf->header->avail.size * sizeof (avail_elem)) >> 1)
  240.             + sizeof (avail_block);
  241.  
  242.   /* Get address in file for new av_size bytes. */
  243.   new_loc = get_elem (av_size, dbf->header->avail.av_table,
  244.               &dbf->header->avail.count);
  245.   if (new_loc.av_size == 0)
  246.     new_loc = get_block (av_size, dbf);
  247.   av_adr = new_loc.av_adr;
  248.  
  249.  
  250.   /* Split the header block. */
  251.   temp = (avail_block *) alloca (av_size);
  252. #ifdef MSDOS
  253.   if (temp == (avail_block *) 0)
  254.     {
  255.       fprintf (stderr, "alloca failed.\n");
  256.       exit (-2);
  257.     }
  258. #endif /* MSDOS */
  259.   /* Set the size to be correct AFTER the pop_avail_block. */
  260.   temp->size = dbf->header->avail.size;
  261.   temp->count = 0;
  262.   temp->next_block = dbf->header->avail.next_block;
  263.   dbf->header->avail.next_block = av_adr;
  264.   for (index = 1; index < dbf->header->avail.count; index++)
  265.     if ( (index & 0x1) == 1)    /* Index is odd. */
  266.       temp->av_table[temp->count++] = dbf->header->avail.av_table[index];
  267.     else
  268.       dbf->header->avail.av_table[index>>1]
  269.     = dbf->header->avail.av_table[index];
  270.  
  271.   /* Update the header avail count to previous size divided by 2. */
  272.   dbf->header->avail.count >>= 1;
  273.  
  274.  
  275.   /* Free the unneeded space. */
  276.   new_loc.av_adr += av_size;
  277.   new_loc.av_size -= av_size;
  278.   _gdbm_free (dbf, new_loc.av_adr, new_loc.av_size);
  279.  
  280.   /* Update the disk. */
  281.   num_bytes = lseek (dbf->desc, av_adr, L_SET);
  282.   if (num_bytes != av_adr) _gdbm_fatal (dbf, "lseek error");
  283. #ifdef MSDOS            /* shut up the compiler!   */
  284.   num_bytes = write (dbf->desc, (char *) temp, av_size);
  285. #else /* not MSDOS */
  286.   num_bytes = write (dbf->desc, temp, av_size);
  287. #endif /* not MSDOS */
  288.  
  289.   if (num_bytes != av_size) _gdbm_fatal (dbf, "write error");
  290.  
  291. }
  292.  
  293.  
  294.  
  295. /* Get_elem returns an element in the AV_TABLE block which is
  296.    larger than SIZE.  AV_COUNT is the number of elements in the
  297.    AV_TABLE.  If an item is found, it extracts it from the AV_TABLE
  298.    and moves the other elements up to fill the space. If no block is 
  299.    found larger than SIZE, get_elem returns a size of zero.  This
  300.    routine does no I/O. */
  301.  
  302. static avail_elem
  303. get_elem (size, av_table, av_count)
  304.      int size;
  305.      avail_elem av_table[];
  306.      int *av_count;
  307. {
  308.   int index;            /* For searching through the avail block. */
  309.   avail_elem val;        /* The default return value. */
  310.  
  311.   /* Initialize default return value. */
  312.   val.av_adr = 0;
  313.   val.av_size = 0;
  314.  
  315.   /* Search for element.  List is sorted by size. */
  316.   index = 0;
  317.   while (index < *av_count && av_table[index].av_size < size)
  318.     {
  319.       index++;
  320.     }
  321.  
  322.   /* Did we find one of the right size? */
  323.   if (index >= *av_count)
  324.     return val;
  325.  
  326.   /* Ok, save that element and move all others up one. */
  327.   val = av_table[index];
  328.   *av_count -= 1;
  329.   while (index < *av_count)
  330.     {
  331.       av_table[index] = av_table[index+1];
  332.       index++;
  333.     }
  334.  
  335.   return val;
  336. }
  337.  
  338.  
  339. /* This routine inserts a single NEW_EL into the AV_TABLE block in
  340.    sorted order. This routine does no I/O. */
  341.  
  342. _gdbm_put_av_elem (new_el, av_table, av_count)
  343.      avail_elem new_el;
  344.      avail_elem av_table[];
  345.      int *av_count;
  346. {
  347.   int index;            /* For searching through the avail block. */
  348.   int index1;
  349.  
  350.   /* Is it too small to deal with? */
  351.   if (new_el.av_size <= IGNORE_SIZE)
  352.     return FALSE; 
  353.  
  354.   /* Search for place to put element.  List is sorted by size. */
  355.   index = 0;
  356.   while (index < *av_count && av_table[index].av_size < new_el.av_size)
  357.     {
  358.       index++;
  359.     }
  360.  
  361.   /* Move all others up one. */
  362.   index1 = *av_count-1;
  363.   while (index1 >= index)
  364.     {
  365.       av_table[index1+1] = av_table[index1];
  366.       index1--;
  367.     }
  368.   
  369.   /* Add the new element. */
  370.   av_table[index] = new_el;
  371.  
  372.   /* Increment the number of elements. */
  373.   *av_count += 1;  
  374.  
  375.   return TRUE;
  376. }
  377.  
  378.  
  379.  
  380. /* Get_block "allocates" new file space and the end of the file.  This is
  381.    done in integral block sizes.  (This helps insure that data smaller than
  382.    one block size is in a single block.)  Enough blocks are allocated to
  383.    make sure the number of bytes allocated in the blocks is larger than SIZE.
  384.    DBF contains the file header that needs updating.  This routine does
  385.    no I/O.  */
  386.  
  387. static avail_elem
  388. get_block (size, dbf)
  389.      int size;
  390.      gdbm_file_info *dbf;
  391. {
  392.   avail_elem val;
  393.  
  394.   /* Need at least one block. */
  395.   val.av_adr  = dbf->header->next_block;
  396.   val.av_size = dbf->header->block_size;
  397.  
  398.   /* Get enough blocks to fit the need. */
  399.   while (val.av_size < size)
  400.     val.av_size += dbf->header->block_size;
  401.  
  402.   /* Update the header and return. */
  403.   dbf->header->next_block += val.av_size;
  404.  
  405.   /* We changed the header. */
  406.   dbf->header_changed = TRUE;
  407.  
  408.   return val;
  409.   
  410. }
  411.  
  412.  
  413. /*  When the header already needs writing, we can make sure the current
  414.     bucket has its avail block as close to 1/2 full as possible. */
  415. static VOID
  416. adjust_bucket_avail (dbf)
  417.      gdbm_file_info *dbf;
  418. {
  419.   int third = BUCKET_AVAIL / 3;
  420.   avail_elem av_el;
  421.  
  422.   /* Can we add more entries to the bucket? */
  423.   if (dbf->bucket->av_count < third)
  424.     {
  425.       if (dbf->header->avail.count > 0)
  426.     {
  427.       dbf->header->avail.count -= 1;
  428.       av_el = dbf->header->avail.av_table[dbf->header->avail.count];
  429.       _gdbm_put_av_elem (av_el, dbf->bucket->bucket_avail,
  430.                  &dbf->bucket->av_count);
  431.       dbf->bucket_changed = TRUE;
  432.     }
  433.       return;
  434.     }
  435.  
  436.   /* Is there too much in the bucket? */
  437.   while (dbf->bucket->av_count > BUCKET_AVAIL-third
  438.      && dbf->header->avail.count < dbf->header->avail.size)
  439.     {
  440.       av_el = get_elem (0, dbf->bucket->bucket_avail, &dbf->bucket->av_count);
  441.       _gdbm_put_av_elem (av_el, dbf->header->avail.av_table,
  442.              &dbf->header->avail.count);
  443.       dbf->bucket_changed = TRUE;
  444.     }
  445. }
  446.  
  447.  
  448.